﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ShopBLL;
using ShopModel;

public partial class controls_PollsControl : System.Web.UI.UserControl
{
    #region PROPERTIES

    int CurrentPollID
    {
        get
        {
            if (ViewState["CurrentPollID"] == null)
                return -1;
            else
                return (int)ViewState["CurrentPollID"];
        }

        set
        {
            ViewState["CurrentPollID"] = value;
        }
    }

    public int TotalVotes
    {
        get
        {
            if (ViewState["TotalVotes"] == null)
                return 0;
            else
                return (int)ViewState["TotalVotes"];
        }

        set
        {
            ViewState["TotalVotes"] = value;
        }
    }

    #endregion PROPERTIES

    #region METHODS

    protected void RefreshPoll(bool ShowAnswers)
    {
        int? CategoryID = null;

        if (Request.QueryString["CategoryId"] != null)
            CategoryID = int.Parse(Request.QueryString["CategoryId"]);

        Poll currentPoll = ReportsManager.Instance.GetPoll(null, CategoryID, (int)CommonTools.CurrentLanguageID);

        if (currentPoll != null)
        {
            CurrentPollID = currentPoll.IDPoll.Value;
            lbMainQuestion.Text = currentPoll.Question;
            TotalVotes = currentPoll.VotesTotal;
            if (ShowAnswers || CheckPollInCookie())
            {
                pnlPollAnswers.Visible = false;
                pnlPollResults.Visible = true;

                rptPollResults.DataSource = currentPoll.Answers;
                rptPollResults.DataBind();
                lblAllVote.Text = Resources.Language.Voted + " : " + currentPoll.VotesTotal.ToString();
            }
            else
            {
                pnlPollAnswers.Visible = true;
                pnlPollResults.Visible = false;

                for (int i = 0; i < currentPoll.Answers.Count; i++)
                    rblPollAnswers.Items.Add(new ListItem(currentPoll.Answers[i].Answer, currentPoll.Answers[i].IDPollAnswer.ToString()));
            }
        }
        else
            upCtrlPoll.Visible = false;
    }

    protected bool CheckPollInCookie()
    {
        return this.Page.Request.Cookies[String.Format("Poll_ID_{0}", CurrentPollID)] != null;
    }

    #endregion METHODS

    #region EVENTS

    protected void btnVote_Click(object sender, EventArgs e)
    {
        if (ShopBLL.ReportsManager.Instance.IncreaseVote(int.Parse(rblPollAnswers.SelectedValue)))
        {
            HttpCookie myCookie = new HttpCookie(String.Format("Poll_ID_{0}", CurrentPollID));
            myCookie.Expires = DateTime.Now.AddDays(15);
            myCookie.Value = rblPollAnswers.SelectedValue;
            Response.Cookies.Add(myCookie);
            RefreshPoll(true);
        }
    }

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        RefreshPoll(true);
    }

    protected void rptPollResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        PollAnswer Answers = (ShopModel.PollAnswer)e.Item.DataItem;
        HtmlGenericControl dPercent = (HtmlGenericControl)e.Item.FindControl("dPercent");

        if (TotalVotes > 0)
        {
            dPercent.Style.Add(HtmlTextWriterStyle.Width, String.Format("{0:F2}%", (100 * (float)Answers.Votes / (float)TotalVotes)).Replace(',', '.'));
            dPercent.InnerText = String.Format("{0:F1}%", 100 * ((float)Answers.Votes / (float)TotalVotes));
        }
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            RefreshPoll(false);
    }

    #endregion PAGE EVENTS

}
